package com.steve.xfood.service.impl;

import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.BestPayService;
import com.steve.xfood.dto.OrderDTO;
import com.steve.xfood.enums.ResultEnum;
import com.steve.xfood.exception.XFoodException;
import com.steve.xfood.service.OrderService;
import com.steve.xfood.service.WeChatPayService;
import com.steve.xfood.util.JsonUtil;
import com.steve.xfood.util.MathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author steve
 * @date 2020/5/15 11:04
 */
@Service
@Slf4j
public class WeChatPayServiceImpl implements WeChatPayService {
    @Autowired
    private BestPayService bestPayService;
    @Autowired
    private OrderService orderService;

    public static final String ORDER_NAME="微信点餐支付订单";

    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest = new PayRequest();
        payRequest.setOrderId(orderDTO.getOrderId());
        //需转为double类型
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOpenid(orderDTO.getBuyerOpenid());
        payRequest.setOrderName(ORDER_NAME);
        //支付类型：支付宝还是微信支付
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【微信支付】 request={}", JsonUtil.toJson(payRequest));
        //返回预付款信息
        PayResponse payResponse = bestPayService.pay(payRequest);
        log.info("【微信支付】 response={}", JsonUtil.toJson(payResponse));

        return payResponse;
    }

    @Override
    public PayResponse notify(String notifyData) {
        //1. 验证签名(sdk提供)
        //2. 支付的状态(sdk提供)
        //3. 支付金额
        //4. 支付人（下单人 == 支付人） -- 根据业务需求
        PayResponse payResponse = bestPayService.asyncNotify(notifyData);
        log.info("【微信支付】 微信异步通知, payResponse={}", JsonUtil.toJson(payResponse));
        //查询订单
        OrderDTO orderDTO = orderService.findOrderById(payResponse.getOrderId());
        //判断订单是否存在
        if (orderDTO == null) {
            log.error("【微信支付】异步通知 订单不存在 orderId={}", payResponse.getOrderId());
            throw new XFoodException(ResultEnum.ORDER_NOT_EXIST);
        }

        //判断金额是否一致
        if (!MathUtil.equals(orderDTO.getOrderAmount().doubleValue(),payResponse.getOrderAmount())) {
            log.error("【微信支付】 异步通知， 订单金额不一致 orderId={},微信通知金额={},系统金额={}"
                    ,payResponse.getOrderId()
                    ,payResponse.getOrderAmount()
                    ,orderDTO.getOrderAmount());
            throw new XFoodException(ResultEnum.WECHAT_PAY_NOTIFY_MONEY_VERIFY_ERROR);
        }

        //修改订单支付状态
        orderService.paid(orderDTO);
        return payResponse;
    }

    @Override
    public void refund(OrderDTO orderDTO) {
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        //支付类型
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【微信退款】 request={}", JsonUtil.toJson(refundRequest));

        //退款结果
        RefundResponse response = bestPayService.refund(refundRequest);
        log.info("【微信退款】 response={}", JsonUtil.toJson(response));
    }


}
